<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Common extends CI_Model
{

    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
        
    }

    public function islogin()
    {
        
        if ($this->session->userdata('logged_in'))
        {
            return TRUE;
        } else {
            redirect('/account/login');
        }
    }

    public $title;
    public $status;
    public $content;

    public function log()
    {
        $data = array(
            'title' => $this->title,
            'url' => $this->uri->uri_string(),
            'content' => $this->content,
            'status' => $this->status,
            'ip_address' => $this->input->ip_address(),
            'come_time' => time()
        );
        
        return $data;
    }

    public function hits($msg)
    {
        if ($this->session->userdata('hits'))
        {
            $hit = $this->session->userdata('hits') + 1;
        } else {
            $hit = 1;
        }

        if ($hit >= 5)
        {
            show_error('禁止继续尝试登录!');
        } else {
            $this->session->set_userdata(array('hits' => $hit));
            show_error($msg);
        }
    }
	    /**
     * 底部分页
     */
    public function bo_page($pagination)
    {
    	extract($pagination);//从数组中将变量导入到当前的符号表
    	if(empty($jumpurl)) { return;}
    	($page<1)?($page=1):'';
    	$pages=ceil($allnum/$total);
    	if($page>$pages){$page=$pages;}
    	if($allnum<1){
    		return false;
    	}elseif($allnum < $total){
    		
    		return $pagination = '<div class="list_page clearfix"><div class="page_wrap"><b class="page_lef"></b>
    								<b class="page_rig">共'.$pages.'页</div></div>';
    	}
    	$pagination = '<div class="list_page clearfix">
    						<div class="page_wrap">
    						<b class="page_lef">
						    <a href="'.$jumpurl.'/'.(($page>1)?($page-1):1).'/'.$uri.'" class="prev"><<</a>';
    	if ($page>3){
    		$pagination .= '<a href="'.$jumpurl.'/'.(($page>1)?($page-2):1).'/'.$uri.'">'.($page-2).'</a>
    						<a href="'.$jumpurl.'/'.(($page>1)?($page-1):1).'/'.$uri.'">'.($page-1).'</a>
    						<a href="#" class="active">'.$page.'</a><b>...</b>
    						<a href="'.$jumpurl.'/'.$pages.'/'.$uri.'">'.$pages.'</a>
						    <a href="'.$jumpurl.'/'.(($page>=$pages)?$pages:($page+1)).'/'.$uri.'" class="next">>></a>
						    </b>
						    <b class="page_rig"><input type="text" value="1" name="inpage" jump="'.$jumpurl.'" uri="'.$uri.'" class="page_inp fontArr" /><input type="button" onclick="forpage()" class="page_btn"  value="确定" /></b>
						    </div>
				    		</div>';
    	}else{
    		$act = ($page<=1)?'class="active"':'';
    		$act2 = ($page==2)?'class="active"':'';
    		$act3 = ($page==3)?'class="active"':'';
    		$pagination .= '<a href="'.$jumpurl.'/1/'.$uri.'"'.$act.' >1</a>
    						<a href="'.$jumpurl.'/2/'.$uri.'"'.$act2.'>2</a>
    						<a href="'.$jumpurl.'/3/'.$uri.'"'.$act3.'>3</a><b>...</b>
    						<a href="'.$jumpurl.'/'.$pages.'/'.$uri.'">'.$pages.'</a>
						    <a href="'.$jumpurl.'/'.(($page>=$pages)?$pages:($page+1)).'/'.$uri.'" class="next">>></a>
						    </b>
						    <b class="page_rig"><input type="text" value="1" name="inpage" jump="'.$jumpurl.'" uri="'.$uri.'" class="page_inp fontArr" /><input type="button" onclick="forpage()" class="page_btn"  value="确定" /></b>
						    </div>
				    		</div>';
    	}
    	return $pagination;
    }


    public function NI($sa,$data)
        {
       //获取分页参数
        $pa=$this->uri->segment(3);
        $this->db->where('status',$sa);
        $rows=$this->db->get("news")->num_rows();//总数
       //加载分页类
        $this->load->library('pagination');
        $config['base_url'] = 'http://law.com/admin/news/';
        $config['total_rows'] = $rows;
        $pagenum = $config['per_page'] = 10;
        $config['first_link'] = '首页'; // 第一页显示   
        $config['last_link'] = '末页'; // 最后一页显示   
        $config['next_link'] = '下一页 >'; // 下一页显示   
        $config['prev_link'] = '< 上一页'; // 上一页显示   
        $config['cur_tag_open'] = ' <a class="current">'; // 当前页开始样式   
        $config['cur_tag_close'] = '</a>';  
        $config['use_page_numbers'] = TRUE;
        $this->pagination->initialize($config);
        $pages= $this->pagination->create_links();
        if (isset($pa) && $pa!='' && $pa>=1) {
             $p=$pa;
        }else {
            $p=1;
        }
        $start = ($p-1)*$pagenum;  //偏移量
        $this->db->select("id,title,content,status,photos,create_time");//保留字段
        $news=$this->db->where('status',$sa)->order_by('create_time','DESC')->get("news",$pagenum,$start)->result_array();
        $data["pages"] = $pages;  
        $data['news']=$news;
        $this->load->view('Admin/header',$data);//公共头
        $this->load->view("Admin/goods",$data);

  }

          //头
        public function header()
        {
            $this->load->view('home/header');
        }
        // 尾
        public function footer()
        {
            $this->load->view('home/footer');
        }
}